<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
$basedir = dirname(__FILE__);
require_once $basedir . '/../../../classes/discovery/uri.php';
require_once $basedir . '/../../../classes/xrd/childelement.php';
require_once $basedir . '/../../../classes/xrd/hostmeta.php';
require_once $basedir . '/../../../classes/xrd/hostmeta/host.php';
require_once $basedir . '/../../../classes/xrd/hostmeta/templatelink.php';
require_once $basedir . '/../../../classes/xrd/hostmeta/templatelink/title.php';
require_once $basedir . '/../../../classes/xrd/lrdd.php';
require_once $basedir . '/../../../classes/xrd/lrdd/subject.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link.php';
require_once $basedir . '/../../../classes/xrd/lrdd/alias.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/atom.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/salmon.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/magicpublickey.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/empty.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/webfingerprofilepage.php';
require_once $basedir . '/../../../classes/xrd.php';

$basedir = dirname(__FILE__);
require_once $basedir . '/../../../classes/atom.php';
require_once $basedir . '/../../../classes/atom/feed.php';
require_once $basedir . '/../../../classes/atom/childelement.php';

require_once $basedir . '/../../../classes/atom/feed/id.php';
require_once $basedir . '/../../../classes/atom/feed/title.php';
require_once $basedir . '/../../../classes/atom/feed/subtitle.php';
require_once $basedir . '/../../../classes/atom/feed/updated.php';
require_once $basedir . '/../../../classes/atom/feed/logo.php';
require_once $basedir . '/../../../classes/atom/feed/generator.php';
require_once $basedir . '/../../../classes/atom/feed/link.php';
require_once $basedir . '/../../../classes/atom/feed/link/alternatehtml.php';
require_once $basedir . '/../../../classes/atom/feed/link/hub.php';
require_once $basedir . '/../../../classes/atom/feed/link/self.php';
require_once $basedir . '/../../../classes/atom/feed/link/salmon.php';
require_once $basedir . '/../../../classes/atom/feed/link/avatar.php';

require_once $basedir . '/../../../classes/atom/feed/author.php';
require_once $basedir . '/../../../classes/atom/feed/author/uri.php';
require_once $basedir . '/../../../classes/atom/feed/author/name.php';

require_once $basedir . '/../../../classes/activitystream.php';
require_once $basedir . '/../../../classes/activitystream/subject.php';
require_once $basedir . '/../../../classes/activitystream/objecttype.php';
require_once $basedir . '/../../../classes/activitystream/object.php';
require_once $basedir . '/../../../classes/activitystream/verb.php';

require_once $basedir . '/../../../classes/atom.php';
require_once $basedir . '/../../../classes/atom/childelement.php';
require_once $basedir . '/../../../classes/atom/entry.php';

require_once $basedir . '/../../../classes/atom/entry/id.php';
require_once $basedir . '/../../../classes/atom/entry/content.php';
require_once $basedir . '/../../../classes/atom/entry/title.php';
require_once $basedir . '/../../../classes/atom/entry/updated.php';
require_once $basedir . '/../../../classes/atom/entry/link.php';
require_once $basedir . '/../../../classes/atom/entry/link/alternatehtml.php';

require_once $basedir . '/../../../classes/atom/entry/author.php';
require_once $basedir . '/../../../classes/atom/entry/author/uri.php';
require_once $basedir . '/../../../classes/atom/entry/author/name.php';

require_once $basedir . '/../../../classes/activitystream.php';
require_once $basedir . '/../../../classes/activitystream/actor.php';
require_once $basedir . '/../../../classes/activitystream/objecttype.php';
require_once $basedir . '/../../../classes/activitystream/verb.php';

require_once $basedir . '/../../../classes/thr.php';
require_once $basedir . '/../../../classes/thr/inreplyto.php';


class Discovery_UriTest extends PHPUnit_Framework_TestCase {
   public function testConstructorOfDiscoveryWithJid() {
        $jid = "acct:tuomas@boobies.com";
        $object = new Discovery_Uri($jid);
        $this->assertEquals("Discovery_Uri", get_class($object));
    }
    
    public function testSuccessfulDiscoveryWithAcctFromIdentica() {
        
        $jid = "acct:tuomas@boobies.com";
        
        $expectedHostMetaXML = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0" xmlns:hm="http://host-meta.net/xrd/1.0">
  <hm:Host>boobies.com</hm:Host>
  <Link rel="lrdd" template="http://boobs.com/xrd?uri={uri}">
    <Title>Resource Descriptor</Title>
  </Link>
</XRD>
';
       $expectedLrddXML = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
  <Subject>http://identi.ca/user/37</Subject>
  <Alias>http://identi.ca/user/37</Alias>
  <Link rel="http://webfinger.net/rel/profile-page" type="text/html" href="http://identi.ca/user/37"/>
  <Link rel="http://schemas.google.com/g/2010#updates-from" type="application/atom+xml" href="http://identi.ca/api/statuses/user_timeline/37.atom"/>
  <Link rel="http://microformats.org/profile/hcard" type="text/html" href="http://identi.ca/rejon/hcard"/>
  <Link rel="http://gmpg.org/xfn/11" type="text/html" href="http://identi.ca/user/37"/>
  <Link rel="describedby" type="application/rdf+xml" href="http://identi.ca/rejon/foaf"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-replies" href="http://identi.ca/main/salmon/user/37"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-mention" href="http://identi.ca/main/salmon/user/37"/>
  <Link rel="magic-public-key" href="data:application/magic-public-key,RSA.gKnrPANtKsidF7I7gvAev0HJ8kKNLrLm_rmPlf0M_g4f5ukqHiMmcCl59qN3u2YGr5Z1ba2SvT7EYWNTaF4AxZCmNR3MRKYVuI2EL7xQZiz1KcWurkiXI-Plm0VbatB30Dg1gosRtWUfjZqWJTJFQyKZXt7NEIhMsfzGNhsuxzk=.AQAB"/>
  <Link rel="http://ostatus.org/schema/1.0/subscribe" template="http://identi.ca/main/ostatussub?profile={uri}"/>
</XRD>
';
       
       $expectedAtomFeed = '<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:georss="http://www.georss.org/georss" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:media="http://purl.org/syndication/atommedia" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:statusnet="http://status.net/schema/api/1/">
 <generator uri="http://status.net" version="0.9.3">StatusNet</generator>
 <id>http://identi.ca/api/statuses/user_timeline/37.atom</id>
 <title>rejon timeline</title>
 <subtitle>Updates from rejon on Identi.ca!</subtitle>
 <logo>http://avatar.identi.ca/37-96-20100311063213.png</logo>
 <updated>2010-08-10T09:32:44+00:00</updated>
<author>
 <name>rejon</name>
 <uri>http://identi.ca/user/37</uri>
</author>
 <link href="http://identi.ca/rejon" rel="alternate" type="text/html"/>
 <link href="http://identi.ca/main/sup#37" rel="http://api.friendfeed.com/2008/03#sup" type="application/json"/>
 <link href="http://identi.ca/main/push/hub" rel="hub"/>
 <link href="http://identi.ca/main/salmon/user/37" rel="http://salmon-protocol.org/ns/salmon-replies"/>
 <link href="http://identi.ca/main/salmon/user/37" rel="http://salmon-protocol.org/ns/salmon-mention"/>
 <link href="http://identi.ca/api/statuses/user_timeline/37.atom" rel="self" type="application/atom+xml"/>
<activity:subject>
 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
 <id>http://identi.ca/user/37</id>
 <title>Jon Phillips</title>
 <link rel="alternate" type="text/html" href="http://identi.ca/rejon"/>
 <link rel="avatar" type="image/png" media:width="192" media:height="192" href="http://avatar.identi.ca/37-192-20100311063213.png"/>
 <link rel="avatar" type="image/png" media:width="96" media:height="96" href="http://avatar.identi.ca/37-96-20100311063213.png"/>
 <link rel="avatar" type="image/png" media:width="48" media:height="48" href="http://avatar.identi.ca/37-48-20100311063213.png"/>
 <link rel="avatar" type="image/png" media:width="24" media:height="24" href="http://avatar.identi.ca/37-24-20100311063213.png"/>
<poco:preferredUsername>rejon</poco:preferredUsername>
<poco:displayName>Jon Phillips</poco:displayName>
<poco:note>http://rejon.org/bio</poco:note>
<poco:address>
 <poco:formatted>San Francisco, USA + Beijing, China</poco:formatted>
</poco:address>
<poco:urls>
 <poco:type>homepage</poco:type>
 <poco:value>http://rejon.org</poco:value>
 <poco:primary>true</poco:primary>
</poco:urls>
</activity:subject>
<entry>
 <title>Did some rapidfire planning with @evanpro today...time to max this puppy out...hardcore august, fall and spring max out :)</title>
 <link rel="alternate" type="text/html" href="http://identi.ca/notice/45644215"/>
 <id>http://identi.ca/notice/45644215</id>
 <published>2010-08-10T06:06:14+00:00</published>
 <updated>2010-08-10T06:06:14+00:00</updated>
 <statusnet:notice_info local_id="45644215" source="gwibber"></statusnet:notice_info>
 <link rel="ostatus:conversation" href="http://identi.ca/conversation/45309395"/>
 <content type="html">Did some rapidfire planning with @evanpro today...time to max this puppy out...hardcore august, fall and spring max out :)</content>
</entry>
<entry>
 <title>Bring da pain. I\'m REcharged!</title>
 <link rel="alternate" type="text/html" href="http://identi.ca/notice/45644179"/>
 <id>http://identi.ca/notice/45644179</id>
 <published>2010-08-10T06:05:38+00:00</published>
 <updated>2010-08-10T06:05:38+00:00</updated>
 <statusnet:notice_info local_id="45644179" source="gwibber"></statusnet:notice_info>
 <link rel="ostatus:conversation" href="http://identi.ca/conversation/45309359"/>
 <content type="html">Bring da pain. I\'m REcharged!</content>
</entry>
<entry>
 <title>How is everyone doing out there?</title>
 <link rel="alternate" type="text/html" href="http://identi.ca/notice/45608813"/>
 <id>http://identi.ca/notice/45608813</id>
 <published>2010-08-09T21:37:58+00:00</published>
 <updated>2010-08-09T21:37:58+00:00</updated>
 <statusnet:notice_info local_id="45608813" source="gwibber"></statusnet:notice_info>
 <link rel="ostatus:conversation" href="http://identi.ca/conversation/45274892"/>
 <content type="html">How is everyone doing out there?</content>
</entry>
<entry>
 <title>Updating with @mlinksva, meeting @davelab6@identi.ca with  at @creativecommons then to !StatusNet !SF...</title>
 <link rel="alternate" type="text/html" href="http://identi.ca/notice/45608277"/>
 <id>http://identi.ca/notice/45608277</id>
 <published>2010-08-09T21:31:46+00:00</published>
 <updated>2010-08-09T21:31:46+00:00</updated>
 <statusnet:notice_info local_id="45608277" source="gwibber"></statusnet:notice_info>
 <link rel="ostatus:conversation" href="http://identi.ca/conversation/45274377"/>
 <link rel="ostatus:attention" href="http://identi.ca/user/8"/>
 <link rel="ostatus:attention" href="http://identi.ca/user/13586"/>
 <link rel="ostatus:attention" href="http://identi.ca/user/14625"/>
 <link rel="ostatus:attention" href="http://identi.ca/group/242/id"/>
 <content type="html">Updating with @&lt;span class=&quot;vcard&quot;&gt;&lt;a href=&quot;http://identi.ca/user/8&quot; class=&quot;url&quot; title=&quot;Mike Linksvayer&quot;&gt;&lt;span class=&quot;fn nickname&quot;&gt;mlinksva&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;, meeting @&lt;span class=&quot;vcard&quot;&gt;&lt;a href=&quot;http://identi.ca/user/14625&quot; class=&quot;url&quot; title=&quot;Dave Crossland&quot;&gt;&lt;span class=&quot;fn nickname&quot;&gt;davelab6&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;@identi.ca with  at @&lt;span class=&quot;vcard&quot;&gt;&lt;a href=&quot;http://identi.ca/user/13586&quot; class=&quot;url&quot; title=&quot;Creative Commons&quot;&gt;&lt;span class=&quot;fn nickname&quot;&gt;creativecommons&lt;/span&gt;&lt;/a&gt;&lt;/span&gt; then to !StatusNet !&lt;span class=&quot;vcard&quot;&gt;&lt;a href=&quot;http://identi.ca/group/242/id&quot; class=&quot;url&quot; title=&quot;San Francisco (sfo)&quot;&gt;&lt;span class=&quot;fn nickname&quot;&gt;SF&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;...</content>
 <category term="sf"></category>
 <category term="statusnet"></category>
</entry>
<entry>
 <title>I know this isn\'t the year of IDEA, ever other year, but! #IDEA2010. Get chinese pirate software makers to distribute inkscape...</title>
 <link rel="alternate" type="text/html" href="http://identi.ca/notice/45596006"/>
 <id>http://identi.ca/notice/45596006</id>
 <published>2010-08-09T18:59:45+00:00</published>
 <updated>2010-08-09T18:59:45+00:00</updated>
 <statusnet:notice_info local_id="45596006" source="gwibber"></statusnet:notice_info>
 <link rel="ostatus:conversation" href="http://identi.ca/conversation/45262515"/>
 <content type="html">I know this isn\'t the year of IDEA, ever other year, but! #&lt;span class=&quot;tag&quot;&gt;&lt;a href=&quot;http://identi.ca/tag/idea2010&quot; rel=&quot;tag&quot;&gt;IDEA2010.&lt;/a&gt;&lt;/span&gt; Get chinese pirate software makers to distribute inkscape...</content>
 <category term="idea2010"></category>
</entry>
<entry>
 <title>Subscribed to TheRealDivaTV http://ping.fm/AIyzY</title>
 <link rel="alternate" type="text/html" href="http://identi.ca/notice/45551465"/>
 <id>http://identi.ca/notice/45551465</id>
 <published>2010-08-09T10:20:23+00:00</published>
 <updated>2010-08-09T10:20:23+00:00</updated>
 <statusnet:notice_info local_id="45551465" source="&lt;a href=&quot;http://ping.fm/&quot; rel=&quot;nofollow&quot;&gt;Ping.fm&lt;/a&gt;" source_link="http://ping.fm/"></statusnet:notice_info>
 <link rel="ostatus:conversation" href="http://identi.ca/conversation/45219144"/>
 <content type="html">Subscribed to TheRealDivaTV &lt;a href=&quot;http://ping.fm/AIyzY&quot; title=&quot;http://www.youtube.com/user/TheRealDivaTV?feature=autoshare&quot; rel=&quot;external&quot;&gt;http://ping.fm/AIyzY&lt;/a&gt;</content>
</entry>
<entry>
 <title>♺ @andyfitz: Confession:   I\'m a skeptic.  Wish I wasn\'t but I dont wish</title>
 <link rel="alternate" type="text/html" href="http://identi.ca/notice/45542045"/>
 <id>http://identi.ca/notice/45542045</id>
 <published>2010-08-09T08:47:39+00:00</published>
 <updated>2010-08-09T08:47:39+00:00</updated>
 <statusnet:notice_info local_id="45542045" source="&lt;a href=&quot;http://launchpad.net/gwibber&quot; rel=&quot;nofollow&quot;&gt;Gwibber&lt;/a&gt;" source_link="http://launchpad.net/gwibber"></statusnet:notice_info>
 <link rel="ostatus:conversation" href="http://identi.ca/conversation/45209814"/>
 <link rel="ostatus:attention" href="http://identi.ca/user/17827"/>
 <content type="html">♺ @&lt;span class=&quot;vcard&quot;&gt;&lt;a href=&quot;http://identi.ca/user/17827&quot; class=&quot;url&quot; title=&quot;Andy Fitzsimon&quot;&gt;&lt;span class=&quot;fn nickname&quot;&gt;andyfitz&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;: Confession:   I\'m a skeptic.  Wish I wasn\'t but I dont wish</content>
</entry>
<entry>
 <title>Pee-wee\'s Big Adventure - Tequila http://ping.fm/FOEFh</title>
 <link rel="alternate" type="text/html" href="http://identi.ca/notice/45495968"/>
 <id>http://identi.ca/notice/45495968</id>
 <published>2010-08-08T22:17:22+00:00</published>
 <updated>2010-08-08T22:17:22+00:00</updated>
 <statusnet:notice_info local_id="45495968" source="&lt;a href=&quot;http://ping.fm/&quot; rel=&quot;nofollow&quot;&gt;Ping.fm&lt;/a&gt;" source_link="http://ping.fm/"></statusnet:notice_info>
 <link rel="ostatus:conversation" href="http://identi.ca/conversation/45164547"/>
 <content type="html">Pee-wee\'s Big Adventure - Tequila &lt;a href=&quot;http://ping.fm/FOEFh&quot; title=&quot;http://www.youtube.com/watch?v=BodXwAYeTfM&amp;amp;feature=autoshare&quot; rel=&quot;external&quot;&gt;http://ping.fm/FOEFh&lt;/a&gt;</content>
</entry>
</feed>
';
        
        //$object = new Discovery_WithJID($jid);
        $mock = $this->getMock('Discovery_Uri', array('fetchURL'), array($jid));
        $mock->debug = false;
        
        $mock->expects($this->at(0))
             ->method('fetchURL')
             ->with($this->equalTo('http://boobies.com/.well-known/host-meta'))
             ->will($this->returnValue($expectedHostMetaXML));
        $mock->expects($this->at(1))
             ->method('fetchURL')
             ->with($this->equalTo('http://boobs.com/xrd?uri=acct%3Atuomas%40boobies.com'))
             ->will($this->returnValue($expectedLrddXML));
        $mock->expects($this->at(2))
             ->method('fetchURL')
             ->with($this->equalTo('http://identi.ca/api/statuses/user_timeline/37.atom'))
             ->will($this->returnValue($expectedAtomFeed));
        $mock->discover();
        
        $this->assertEquals("http://identi.ca/main/push/hub", $mock->getHubUri());
        $this->assertEquals("http://identi.ca/main/salmon/user/37", $mock->getSalmonUri());
        $this->assertEquals("http://identi.ca/api/statuses/user_timeline/37.atom", $mock->getAtomFeedUri());
        $this->assertEquals("data:application/magic-public-key,RSA.gKnrPANtKsidF7I7gvAev0HJ8kKNLrLm_rmPlf0M_g4f5ukqHiMmcCl59qN3u2YGr5Z1ba2SvT7EYWNTaF4AxZCmNR3MRKYVuI2EL7xQZiz1KcWurkiXI-Plm0VbatB30Dg1gosRtWUfjZqWJTJFQyKZXt7NEIhMsfzGNhsuxzk=.AQAB", $mock->getMagicPublicKey());
        $this->assertEquals("http://avatar.identi.ca/37-192-20100311063213.png", $mock->getLogo());
        $this->assertEquals("http://identi.ca/user/37", $mock->getWebfingerProfilePageUri());
        $this->assertEquals("Jon Phillips", $mock->getName());
    }
    
    public function testDiscoveryWithJidButFailsInMetaDataDomainValidation() {
        
        $jid = "tuomas@boobies.com";
        
        $expectedHostMetaXML = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0" xmlns:hm="http://host-meta.net/xrd/1.0">
  <hm:Host>boobs.com</hm:Host>
  <Link rel="lrdd" template="http://boobs.com/xrd?uri={uri}">
    <Title>Resource Descriptor</Title>
  </Link>
</XRD>
';
        
        //$object = new Discovery_WithJID($jid);
        $mock = $this->getMock('Discovery_Uri', array('fetchURL'), array($jid));
        
        $mock->expects($this->at(0))
             ->method('fetchURL')
             ->with($this->equalTo('http://boobies.com/.well-known/host-meta'))
             ->will($this->returnValue($expectedHostMetaXML));
        $mock->expects($this->at(1))
             ->method('fetchURL')
             ->with($this->equalTo('acct:tuomas@boobies.com'))
             ->will($this->returnValue(false));
        $mock->debug = false;         
        $this->assertEquals(false, $mock->discover());
    }
}